/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.chlitina.store.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * 
 * @author ThinkGem
 * @version 2013-3-15
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	private static String[] parsePatterns = { "yyyy-MM-dd",
			"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
			"yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd'T'HH:mm:ss'Z'" };

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	public static String formatDate(Date date, String format) {
		if (date == null) {
			return "";
		}
		return DateFormatUtils.format(date, format);
	}

	public static String getAge(Date date) {
		long pastDays = pastDays(date);
		long age = 0;
		if (pastDays > 0) {
			age = (long) Math.ceil(pastDays / 365);
		}
		return age > 0 ? age + 1 + "" : "未填写";
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * 日期型字符串转化为日期 格式（"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd",
	 * "yyyy/MM/dd HH:mm:ss" ）
	 */
	public static Date parseDate(String str) {
		try {
			return parseDate(str, parsePatterns);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取经过的天数
	 * 
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = System.currentTimeMillis() - date.getTime();
		return t / (24 * 60 * 60 * 1000);
	}

	/**
	 * 获取剩余的天数
	 * 
	 * @param date
	 * @return
	 */
	public static long remainDays(Date date) {
		long t = date.getTime() - System.currentTimeMillis();
		return t / (24 * 60 * 60 * 1000);
	}

	/**
	 * 当月第一天
	 * 
	 * @return
	 */
	public static Date getFirstDay() {
		Calendar cal = Calendar.getInstance();
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		return getBeginOfDay(cal.getTime());
	}

	/**
	 * 当月最后一天
	 * 
	 * @return
	 */
	public static Date getLastDay() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		return getEndOfDay(cal.getTime());
	}

	/**
	 * 计算两个日期的间隔月数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int calculateDurationMonth(Date start, Date end) {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		int offsetYear = endCalendar.get(Calendar.YEAR)
				- startCalendar.get(Calendar.YEAR);
		int offsetMonth = endCalendar.get(Calendar.MONTH)
				- startCalendar.get(Calendar.MONTH);
		return offsetMonth + offsetYear * 12;
	}
	public static Date calculateDurationStartDay(Date endDate, int cardDuration) {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		endCalendar.add(Calendar.MONTH, 0-cardDuration);
		return endCalendar.getTime();
	}
	
	public static Date appendMonth(Date current,Integer appendMonth){
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(current);
		int offsetYear=appendMonth/12;
		int offsetMonth=appendMonth%12;
		endCalendar.set(Calendar.YEAR, endCalendar.get(Calendar.YEAR)+offsetYear);
		endCalendar.set(Calendar.MONTH, endCalendar.get(Calendar.MONTH)+offsetMonth);
		return endCalendar.getTime();
	}
	
	public static Date appendSeconds(Date current, long seconds){
		long time = current.getTime() + seconds*1000;
		return new Date(time);
	}
	
	public static Date appendSeconds(long seconds){
		long current = System.currentTimeMillis();
		long target=current + seconds*1000;
		return new Date(target);
	}
	/**
	 * 获得某天的最早时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getBeginOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static Date getYestoday() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 获得某天的最晚时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getEndOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 判断日期是否是今天
	 * 
	 * @param date
	 * @return
	 */
	public static boolean ifToday(Date date) {
		Calendar beginCal = Calendar.getInstance();
		beginCal.setTime(date);
		Calendar tadayCal = Calendar.getInstance();
		tadayCal.setTime(new Date());
		return formatDate(beginCal.getTime()).equals(
				formatDate(tadayCal.getTime()));
	}

	/**
	 * 判断产品或护理是否快到期
	 * 
	 * @param date
	 * @return
	 */
	public static boolean ifExpire(Date begin, int duration, int day) {
		Calendar beginCal = Calendar.getInstance();
		beginCal.setTime(begin);
		beginCal.add(Calendar.DAY_OF_MONTH, duration);
		Calendar tadayCal = Calendar.getInstance();
		tadayCal.setTime(new Date());
		tadayCal.add(Calendar.DAY_OF_MONTH, day);
		return formatDate(beginCal.getTime()).equals(
				formatDate(tadayCal.getTime()));
	}

	/**
	 * 将java.util.Date转换成java.sql.Date
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static java.sql.Date parseToSqlDate(Date date) {
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}
	/**
	 * 获取几天前的时间
	 * 
	 * @param int
	 * @return
	 */
	public static String getPastDate(int num) {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DAY_OF_MONTH, -num);
		String day = formatDate(today.getTime(), "yyyy-MM-dd");
		return day;
	}

	/**
	 * 将java.util.Date转换成java.sql.Timestamp
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Timestamp parseToSqlDateTime(Date date) {
		Timestamp ts = new Timestamp(date.getTime());
		return ts;
	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		Date current = new Date();
		System.err.println(formatDate(current,"yyyy-MM-dd HH:mm:ss"));
		System.err.println(formatDate(calculateDurationStartDay(current,1),"yyyy-MM-dd HH:mm:ss"));
	}

	
}
